var hdialog = function (element, config) {
    this.config = {
        //内容区高度,整体拟态框的宽度
        width: "200", height: "250", scroll: false
    }
    this.config = config || this.config;
    this.el = document.querySelector(`.${element}`);
    this.overlay;
    this.init();
}
/* 初始化弹窗 */
hdialog.prototype.init = function () {
    this.initOverlay();
    //init config 
    this.el.style.cssText += `width:${this.config.width}px;`;
    this.el.querySelector('.hdialog-cont').style.cssText += `height:${this.config.height}px;`;
    this.resize();//定义窗口的位置
    window.onresize = () => {
        console.log('on page resize');
        this.resize();
    }
    this.el.style.visibility = 'visible';
    this.el.style.display = 'none';

    //
    if (this.config.scroll) {
        this.openScroll();
    } else {
        this.closeScroll();
    }
    this.cancel(() => { this.close() });

}
//添加关闭事件
hdialog.prototype.rightClose = function (call) {
    let close =  this.el.querySelector('.hdialog-close-botton');
    if (close) {
        close.onclick = () => {
            call();
        }
    }
}
/* 
 关闭窗口
*/
hdialog.prototype.close = function () {
    this.el.style.display = 'none';
    if (!this.overlay) {
        this.overlay = document.querySelector('.hdialogOverlay');
    }
    this.overlay.style.display = 'none';
}
/* 打开窗口 */
hdialog.prototype.open = function () {

    this.resize(true);
    if (!this.overlay) {
        this.overlay = document.getElementsByClassName('hdialogOverlay')[0];
    }
    this.overlay.style.display = 'block';
}
/* 初始化蒙版 */
hdialog.prototype.initOverlay = function () {
    let olay = document.createElement('div');
    olay.classList.add('hdialogOverlay');
    olay.style.height = window.innerHeight + 'px';
    //console.dir( olay.style.height)
    // console.log(document.body,olay,this.el)
    this.el.parentNode.insertBefore(olay, this.el);
    olay.style.display = 'none';
}
/* 确认按钮点击事件控制 */
hdialog.prototype.confirm = function (call) {
    let confirm = this.el.querySelector('.hdialog-button-confirm');
    if (confirm) {
        confirm.onclick = call;
    }
}
/* 取消按钮点击事件控制 */
hdialog.prototype.cancel = function (call) {
    let cancel = this.el.querySelector('.hdialog-button-cancel');
    if (cancel) {
        cancel.onclick = call;
    }
}
/* 页面大小发生变化控制 */
hdialog.prototype.resize = function (state) {
    if (state) {
        this.el.style.display = 'block';
    }
    let left = (window.innerWidth - this.el.clientWidth) / 2;
    let top = (window.innerHeight - this.el.clientHeight) / 2;

    //设置弹窗的位置
    this.el.style.left = left + "px";
    this.el.style.top = top + "px";

    if (!this.hdialogOverlay) {
        this.hdialogOverlay = document.querySelector('.hdialogOverlay');
    }
    this.hdialogOverlay.style.width = window.innerWidth + "px";
    this.hdialogOverlay.style.height = window.innerHeight + "px";
}
hdialog.prototype.openScroll = function () {
    this.el.querySelector('.hdialog-cont').style.overflowY = 'scroll';
}
hdialog.prototype.closeScroll = function () {
    this.el.querySelector('.hdialog-cont').style.overflow = 'auto';
}
module.exports.hdialog = hdialog


